//
//  FileCell.m
//  FuelLog
//
//  Created by Pure Mass Communications on 4/5/09.
//  Copyright 2009 . All rights reserved.
//
//  Placed under GPL v2 with the kind permission of Pure Mass Communications
//
//  Modified to make compile under SDK 3.1 by Olaf Greck in May 2010
//
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#import "FileCell.h"
#import "FilesViewController.h"
#import "GDataEntryDocBase.h"
#import "GDataCategory.h"
#import "GDataEntryBase.h"

@implementation FileCell

@synthesize gfile, master;

# pragma mark UITableViewCell

- (id)initWithFrame:(CGRect)frame // reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithFrame:frame ]) // reuseIdentifier:reuseIdentifier]) 
	{
		self.target = self;
		
		// cell's title label
		titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
		titleLabel.backgroundColor = [UIColor clearColor];
		titleLabel.opaque = NO;
		titleLabel.textColor = [UIColor blackColor];
		titleLabel.highlightedTextColor = [UIColor whiteColor];
		titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
		[self.contentView addSubview:titleLabel];
				 
		// cell's import button
		importButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
		importButton.frame = CGRectZero;
		importButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
		importButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
		[importButton addTarget:self action:@selector(import:) forControlEvents:UIControlEventTouchUpInside];
		importButton.backgroundColor = [UIColor clearColor];
		[importButton setTitle:@"import" forState:UIControlStateNormal];
		[self.contentView addSubview:importButton];
	}
	return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
	
    [super setSelected:selected animated:animated];
	
    // Configure the view for the selected state
}

- (void)layoutSubviews {
	[super layoutSubviews];
	
    CGRect contentRect = [self.contentView bounds];
	
	CGRect frame = CGRectMake(contentRect.origin.x + 10.0, 8.0, contentRect.size.width, 30.0);
	titleLabel.frame = frame;
	titleLabel.text = [[self.gfile title] stringValue];
    NSArray *categories;
    categories = [GDataCategory categoriesWithScheme:kGDataCategoryScheme
                                      fromCategories:[gfile categories]];
	int i, len = [categories count];
	for (i = 0; i < len; ++i) {
		GDataCategory *cat = (GDataCategory *)[categories objectAtIndex:i];
		if ([[cat label] isEqual:@"spreadsheet"]) {
			break;
		}
	}
	if (i < len) {
		frame = CGRectMake(contentRect.origin.x + 249.0, 9.0, 64.0, 25.0);
		importButton.frame = frame;
		importButton.hidden = NO;
	} else {
		importButton.hidden = YES;
	}
}

- (void)dealloc
{
	[titleLabel release];
	[importButton release];
    [super dealloc];
}

- (void)import:(id)sender {
	// importButton.selected = NO;
	// [importButton setNeedsDisplay];
	[master importCar:gfile];
}

@end
